package com.example.chuntiao.cophook.brodcast;

import android.Manifest;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.NotificationCompat;
import android.util.Log;

import com.example.chuntiao.cophook.R;
import com.example.chuntiao.cophook.mytools.PhoneTool;
import com.example.chuntiao.cophook.ui.MyInfoActivity;

import static android.content.Context.MODE_PRIVATE;

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "MyReceiver";
    int a=0;

    @Override
    public void onReceive(final Context context, Intent intent) {
        Log.d(TAG, "onReceive: "+"开启"+intent.getAction());
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE)
                == PackageManager.PERMISSION_GRANTED) {

            Log.d(TAG, "onReceive: "+"权限成功");
            SharedPreferences pref=context.getSharedPreferences("data", MODE_PRIVATE);
            Log.d(TAG, "onReceive:pref "+pref);


            final String oldImsi=pref.getString("imsi",null);
            Log.d(TAG, "onReceive:oldImsi "+oldImsi);

            final String newImsi = PhoneTool.getImsi(context);

            Log.d(TAG, "onReceive:newImsi "+newImsi);
            if (oldImsi != null && !oldImsi.equals(newImsi)) {
//if(true){
                //Intent defaultIntent = context.getPackageManager().
                  //      getLaunchIntentForPackage("com.example.chuntiao.cophook");
                Intent defaultIntent=new Intent(Intent.ACTION_MAIN);
                defaultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                defaultIntent.setClass(context.getApplicationContext(),MyInfoActivity.class);
                defaultIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED|Intent.FLAG_ACTIVITY_NEW_TASK);
                PendingIntent pendingIntent=PendingIntent.getBroadcast(context.getApplicationContext(),0,defaultIntent,0);

                Notification notif=new NotificationCompat.Builder(context)
                        .setTicker("请更新信息")
                        .setContentTitle("sim卡")
                        .setAutoCancel(false)
                        .setContentText("检测到 sim 卡已经更换，请及时更新个人信息")
                        .setDefaults(NotificationCompat.DEFAULT_ALL)
                        .setSmallIcon(R.drawable.ic_action_book)
                        .setColor(ContextCompat.getColor(context,R.color.colorPrimary))
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setWhen(System.currentTimeMillis())
                        .setContentIntent(pendingIntent)
                        .build();

                NotificationManager manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
                assert manager != null;
                manager.notify(1,notif);
            }

        }

    }
}
